package com.example.connecting_dots_android;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;

public class HighScores extends Activity {

	private boolean mIsMute;
	private TextView mHighscores;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_high_scores);
		init();
		settings();

		StringBuffer highscoresString = readHighscoresFromFIle();
		mHighscores = (TextView) findViewById(R.id.highscores);
		mHighscores.setText(highscoresString.toString());

		// Show the Up button in the action bar.
		setupActionBar();
	}

	/**
	 * Method to read saved highscores from a file
	 * */
	private StringBuffer readHighscoresFromFIle() {
		FileInputStream fis;
		StringBuffer str = new StringBuffer();
		String newline = System.getProperty(Constants.SEPARATOR);
		try {
			fis = openFileInput(Constants.HIGHSCORE_FILE_NAME);
			InputStreamReader isr = new InputStreamReader(fis);
			BufferedReader bufferedReader = new BufferedReader(isr);
			String line;
			int i = 1;
			while ((line = bufferedReader.readLine()) != null
					&& !line.equals("")) {
				String[] temp = line.split(",");
				str.append(i + ". " + temp[0] + " - " + temp[1] + newline);
				i++;
			}
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		;
		return str;
	}

	/**
	 * method to manage the background image and the music
	 * */
	private void settings() {
		Utils.setBackgroundImage(this, findViewById(R.id.highScoresLayout));
		
		if (MainActivity.mServ != null && mIsMute
				&& !MainActivity.mServ.isPlaying()) {
			MainActivity.mServ.resume();
		}
	}

	/**
	 * method to initialize class members
	 * */
	private void init() {
		mIsMute = Utils.getIsMute(this);

	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.high_scores, menu);
		return true;
	}

	@Override
	protected void onResume() {
		super.onResume();
		init();
		Utils.setBackgroundImage(this, findViewById(R.id.highScoresLayout));
		settings();
	}

	@Override
	protected void onPause() {
		super.onPause();
		init();
		if (MainActivity.mServ != null && mIsMute) {
			MainActivity.mServ.pause();
		}
	}
}
